package com.gugee.ins.common.dal.service.blogger.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.blogger.InsBloggerRelationExtMapper;
import com.gugee.ins.common.dal.service.blogger.InsBloggerRelationDao;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerRelation;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * InsBloggerRelationDaoImpl
 * @author Huangxin
 */
@Slf4j
@Service
public class InsBloggerRelationDaoImpl extends ServiceImpl<InsBloggerRelationExtMapper, InsBloggerRelation> implements InsBloggerRelationDao {

    @Override
    public void batchInsert(List<InsBloggerRelation> insBloggerRelationList){

        try{
            baseMapper.batchInsert(insBloggerRelationList);
        }catch (Exception e){
            e.printStackTrace();
            log.debug("batch insert ins_blogger_relation failed ,try insert one by one, msg : {} ",e.getMessage());
            insBloggerRelationList.forEach(insBloggerRelation -> {
                try{
                    baseMapper.insert(insBloggerRelation);
                }catch (Exception ex){
                    e.printStackTrace();
                    log.info("insert ins_blogger_relation failed ,uid : {} , msg : {} "
                            ,insBloggerRelation.getUid(),ex.getMessage());
                }
            });
        }
    }

    /**
     * 从ins_blogger_relation中查询符合时间段内的数据封装成消息
     * @param startTime 开始时间戳
     * @param endTime 结束时间戳
     * @return 消息列表
     */
    public List<InsBloggerMessage> produceMsgFromBloggerRelation(long startTime, long endTime) {
       return  baseMapper.produceMsgFromBloggerRelation(startTime,endTime);
    }
}
